package com.finger.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.finger.commonutils.R;
import com.finger.eduservice.exception.GuliException;
import com.finger.vod.service.VodService;
import com.finger.vod.util.ConstantVodUtils;
import com.finger.vod.util.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RequestMapping("/eduvod/filevod")
@CrossOrigin
@RestController
public class VodController {
    @Autowired
    private VodService vodService;

    @PostMapping("/uploadVod")
    public R uploadVod(MultipartFile file){
        return vodService.uploadVod(file);
    }

    @DeleteMapping("/deleteVod/{videoId}")
    public R deleteVod(@PathVariable String videoId){
        try{
            vodService.deleteVod(videoId);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }
    @DeleteMapping("/deleteVodBatch")
    public R deleteBatch(@RequestParam("ids") List<String> ids){
        try {
            vodService.deleteVodBatch(ids);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/getPlayAuth/{videoSourceId}")
    public R getPlayAuth(@PathVariable String videoSourceId){
        try {
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

            //设置请求参数
            request.setVideoId(videoSourceId);
            //获取请求响应
            response = client.getAcsResponse(request);

            //播放凭证
            return R.ok().data("playAuth",response.getPlayAuth());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"获取视频资源失败");
        }
    }

}
